package com.smartwebx.ha.biz.assign.impl;

import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.smartwebx.ha.biz.assign.HaUserManager;
import com.smartwebx.ha.dal.assign.bean.HaUser;
import com.smartwebx.ha.dal.assign.bean.HaUserExample;
import com.smartwebx.ha.dal.assign.bean.HaUserExample.Criteria;
import com.smartwebx.ha.dal.assign.dao.HaUserMapper;
import com.smartwebx.ha.dal.util.CommonsUtil;
import com.smartwebx.ha.usebean.Paging;
import com.smartwebx.ha.usebean.TableData;

@Service
public class HaUserManagerImpl implements HaUserManager {

	@Autowired
	private HaUserMapper haUserDao;
	@Autowired
	private HaUserMapper haUserMapper;
	@Override
	public Entry<Integer, HaUser> loginUser(String userName, String pass) {
		HaUserExample userExample = new HaUserExample();
		Criteria criter = userExample.createCriteria();
		// 密码md5 加密
		String md5Pass = DigestUtils.md5Hex(pass);
		criter.andNameEqualTo(userName).andPassEqualTo(md5Pass);

		// 根据用户名密码查询用户
		List<HaUser> haUsers = haUserDao.selectByExample(userExample);
		HaUser user = Iterables.getFirst(haUsers, null);
		int state = 0;
		if (user == null) {// 登陆失败，用户名或密码错误 或者用户被锁定
			state = 0;
		} else if (BooleanUtils.isFalse(user.getIsLogin())) {
			state = -1;
		} else {
			state = 1;
		}
		return Maps.immutableEntry(state, user);
	}

	@Override
	public TableData<HaUser> queryUserByPage(Paging page,Integer groupId,String code) {
		HaUserExample userExample = new HaUserExample();
		Criteria criter = userExample.createCriteria();
		userExample.setOrderByClause(Objects.firstNonNull(page.getOrder(),
				"id desc"));

		// 用户名，模糊查询
		String name = page.getKey1();
		if (name != null)
			criter.andNameLike("%" + name + "%");
		// pageIndex 从 0 开始，所以要 +1
		PageHelper.startPage(page.getPageIndex() + 1, page.getPageSize());
		if(groupId==2){
			criter.andCodeLike("%" +code.substring(0, 4)+"%");
		}else if(groupId==3){
			criter.andCodeLike("%" +code.substring(0, 5)+"%");
		}
		List<HaUser> users = haUserDao.selectByExample(userExample);
		return new TableData<HaUser>(users);
	}

	@Override
	public boolean changePass(int userId, String newPass) {
		String newMd5Pass = DigestUtils.md5Hex(newPass);
		HaUser changeUserPass = new HaUser();
		changeUserPass.setPass(newMd5Pass);
		changeUserPass.setId(userId);

		int state = haUserDao.updateByPrimaryKeySelective(changeUserPass);

		return CommonsUtil.checkedTrueOrFalse(state);
	}

	@Override
	public boolean resetPass(int userId) {
		return changePass(userId, "123456");
	}

	@Override
	public boolean updateUser(HaUser newUser) {
		newUser.setUpdateTime(new Date());
		String pass=newUser.getPass();
		if(pass==null||"".equals(pass)){
			pass = "123456";
		}
		String md5Pass = DigestUtils.md5Hex(pass);
		newUser.setPass(md5Pass);
		int state = haUserDao.updateByPrimaryKeySelective(newUser);

		return CommonsUtil.checkedTrueOrFalse(state);
	}

	@Override
	public boolean addUser(HaUser user) {
		String pass = "admin";
		String md5Pass = DigestUtils.md5Hex(pass);
		user.setPass(md5Pass);

		user.setCreateTime(new Date());
		user.setUpdateTime(new Date());

		int state = haUserDao.insert(user);

		return CommonsUtil.checkedTrueOrFalse(state);
	}

	@Override
	public boolean checkName(String name) {
		HaUserExample userExample = new HaUserExample();
		Criteria crite = userExample.createCriteria();
		crite.andNameEqualTo(name);
		// 根据用户名搜索符合的记录
		List<HaUser> queryByName = haUserDao.selectByExample(userExample);

		return queryByName.isEmpty();
	}

	@Override
	public boolean lockedUser(int userId) {
		return this.lockUnLockUser(userId, false);
	}

	@Override
	public boolean unLockedUser(int userId) {
		return this.lockUnLockUser(userId, true);
	}

	private boolean lockUnLockUser(int userId, boolean sign) {
		HaUser haUser = new HaUser();
		haUser.setIsLogin(sign);
		haUser.setId(userId);
		int state = haUserDao.updateByPrimaryKeySelective(haUser);

		return CommonsUtil.checkedTrueOrFalse(state);
	}

	@Override
	public boolean delUser(int userId) {
		int state = haUserDao.deleteByPrimaryKey(userId);
		return CommonsUtil.checkedTrueOrFalse(state);
	}

	@Override
	public HaUser findById(int userId) {
		return haUserDao.selectByPrimaryKey(userId);
	}

	@Override
	public int addUserGetId(HaUser user) {
		this.addUser(user);
		Integer id = user.getId();
		return id;
	}

	@Override
	public List<HaUser> select(int haAreaId) {
		HaUserExample haUserExample = new HaUserExample();
		com.smartwebx.ha.dal.assign.bean.HaUserExample.Criteria haUserCriteria = haUserExample.createCriteria();
		haUserCriteria.andAreaIdEqualTo(haAreaId);
		return this.haUserMapper.selectByExample(haUserExample);
	}

	@Override
	public List<HaUser> getUserData(String type, String param1) {
		HaUserExample haUserExample = new HaUserExample();
		com.smartwebx.ha.dal.assign.bean.HaUserExample.Criteria haUsercriteria = haUserExample.createCriteria();
		if("areaId".equals(type)){
			haUsercriteria.andAreaIdEqualTo(Integer.parseInt(param1));
		}else if("code".equals(type)){
			haUsercriteria.andCodeEqualTo(param1);
		}
		List<HaUser> haUserLists = this.haUserMapper.selectByExample(haUserExample);
		return haUserLists;
	}
}
